﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
namespace QUANLYDIEM
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        ArrayList DSMH;
        private void LayDSMH()
        {
            Database.CreateCommand("select MAMH from MONHOC");
            SqlDataReader reader = Database.ExecuteReader();
            while (reader.Read())
            { 
                DSMH.Add(reader[0].ToString());
            }
            reader.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Database.Tao_ket_noi(@"Data Source=HUYNHPHUC-PC;AttachDbFilename=" + Application.StartupPath + @"\DB\QLDSV.mdf;Integrated Security=True;Connect Timeout=30;");
            Database.Tao_ket_noi(@"Data Source=HUYNHPHUC-PC;Initial Catalog=QLDSV;Integrated Security=True");
            Database.CreateCommand("select * from DIEMTHI");
            dataGridView1.DataSource = Database.ExecuteTable();
        }
        private String LayMaMH()
        {
            String Mamh = "";
            Database.CreateCommand("select TOP 1 MAMH from MONHOC DESC");
            SqlDataReader reader = Database.ExecuteReader();
            while (reader.Read())
                Mamh = reader[0].ToString();
            if (Mamh == "") Mamh = "MH01";
            return Mamh;
        }
        private int CountMonHoc()
        {
            int Count = 0;
            Database.CreateCommand("select count(MAMH) from MONHOC");
            SqlDataReader reader = Database.ExecuteReader();
            while (reader.Read())
                Count =Convert.ToInt32(reader[0].ToString());
            reader.Close();
            return Count;
        }
        private void ResetData()
        {
            Database.CreateCommand("ResetDATA", CommandType.StoredProcedure);
            Database.ExecuteNonQuery();
        }
        private void ThemMH(string Mamh, string TenMH)
        {
            Database.CreateCommand("ThemMH",CommandType.StoredProcedure);
            Database.AddParameter("@MAMH", DbType.String, Mamh);
            Database.AddParameter("@TENMH", DbType.String, TenMH);
            Database.ExecuteNonQuery();
        }
        private void NhapDiemThi(string Masv,string Mamh,int LanThi,float Diem)
        {
            Database.CreateCommand("NHAPDIEM", CommandType.StoredProcedure);
            Database.AddParameter("@MASV", DbType.String, Masv);
            Database.AddParameter("@MAMH", DbType.String, Mamh);
            Database.AddParameter("@LAN", DbType.Int16, LanThi);
            Database.AddParameter("@DIEM", DbType.Decimal, Diem);
            Database.ExecuteNonQuery();
        }

        private void  NhapDiem1SV(string MASV)
        {
            float DiemLan1, DiemLan2;
            for (int i = 0; i < DSMH.Count; i++)
            {
                DiemLan1 = GetRandomNumber(0, 11);
                NhapDiemThi(MASV, DSMH[i].ToString(), 1, DiemLan1);
                if (DiemLan1 < 5)
                {
                    DiemLan2 = GetRandomNumber(0, 11);
                    NhapDiemThi(MASV, DSMH[i].ToString(),2, DiemLan2);
                }
            }
        }
        private void btnSinhDLMH_Click(object sender, EventArgs e)
        {

            int SoMH = 12;
            for (int i = 1; i <= SoMH; i++)
            {
                ThemMH("MH" + i.ToString().PadLeft(2, '0'),"Môn học "+i);
            }
            MessageBox.Show("Sinh dữ liệu thành công");
        }
        float GetRandomNumber(int min,int max)
        {
            Random rnd = new Random((int)DateTime.Now.Ticks);
            return rnd.Next(min, max);


        }
        private void btnSinhDLDiem_Click(object sender, EventArgs e)
        {
            int SoSinhVien = 500;
            DSMH = new ArrayList();
            LayDSMH();
            for (int i = 1; i <=SoSinhVien; i++)
            { 
                NhapDiem1SV("SV"+i.ToString().PadLeft(4,'0'));
            }
            MessageBox.Show("Sinh dữ liệu thành công");
        }

        private void btnResetData_Click(object sender, EventArgs e)
        {
            ResetData();
            MessageBox.Show("Reset data thành công");
        }
    }
}
